package com.example.sunmiv1.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.example.sunmiv1.R;
import com.example.sunmiv1.activity.StringCodeOperationActivity;
import com.example.sunmiv1.entity.InventoryInfoBean;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.ACache;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


// 細節
public class InventoryInfoAdapter extends BaseAdapter {

    private Context context;
    private List<InventoryInfoBean.DataBean> list;
    private ViewHolder viewHolder;
    private ACache aCache;

    public InventoryInfoAdapter(Context context, List<InventoryInfoBean.DataBean> list) {
        this.context = context;
        this.list = list;
        aCache = ACache.get(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.inventory_info_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        InventoryInfoBean.DataBean dataBean = list.get(position);
        Tools.hideViewInItem(dataBean.getSku_name(), viewHolder.sku);
        viewHolder.nameInventoryInfo.setText(dataBean.getGoods_name());
        if ("2".equals(dataBean.getAttribute_state())) {
            viewHolder.skuInventoryInfo.setText(dataBean.getSku_name());
            viewHolder.barcodeInventoryInfo.setText(dataBean.getSku_code());
            viewHolder.freightNumberInventoryInfo.setText(dataBean.getSku_goods_number());
        } else {
            viewHolder.skuLinInventoryInfo.setVisibility(View.GONE);
            viewHolder.barcodeInventoryInfo.setText(dataBean.getGoods_code());
            viewHolder.freightNumberInventoryInfo.setText(dataBean.getGoods_number());
        }
        Tools.hideViewInItem(dataBean.getCompany(), viewHolder.unitInventoryInfo);
        viewHolder.oldStockInventoryInfo.setText(dataBean.getGoods_num() + "");
        viewHolder.inventoryInventoryInfo.setText(dataBean.getInventory_num() + "");
        final int index = position;
        if("1".equals(dataBean.getSerial_number_state())){
            viewHolder.inventoryInventoryInfo.setTextColor(ContextCompat.getColor(context,R.color.blue_3293FA));
        }else{
            viewHolder.inventoryInventoryInfo.setTextColor(ContextCompat.getColor(context,R.color.text_666));
        }
        viewHolder.inventoryInventoryInfo.setOnClickListener(v -> {
            choose.click(index);
        });
        viewHolder.purchasePriceInventoryInfo.setText(dataBean.getCost_price() + "");
        viewHolder.totalInventoryInfo.setText(dataBean.getSubtotal() + "");
        viewHolder.numInventoryInfo.setText(dataBean.getNum() + "");
        if ("2".equals(aCache.getAsString("supplier_type")) && !TextUtils.isEmpty(dataBean.getPurchase_order_number())) {
            viewHolder.llOrderNumber.setVisibility(View.VISIBLE);
            viewHolder.infoInventoryOrderNumber.setText(dataBean.getPurchase_order_number());
        } else {
            viewHolder.llOrderNumber.setVisibility(View.GONE);
        }
        return convertView;
    }


    private Choose choose;
    public void setChoose(Choose choose){
        this.choose = choose;
    }

    public interface Choose{
        void click(int index);
    }

    static class ViewHolder {
        @BindView(R.id.name_inventoryInfo)
        TextView nameInventoryInfo;
        @BindView(R.id.sku_inventoryInfo)
        TextView skuInventoryInfo;
        @BindView(R.id.skuLin_inventoryInfo)
        LinearLayout skuLinInventoryInfo;
        @BindView(R.id.sku)
        TextView sku;
        @BindView(R.id.barcode_inventoryInfo)
        TextView barcodeInventoryInfo;
        @BindView(R.id.freightNumber_inventoryInfo)
        TextView freightNumberInventoryInfo;
        @BindView(R.id.unit_inventoryInfo)
        TextView unitInventoryInfo;
        @BindView(R.id.oldStock_inventoryInfo)
        TextView oldStockInventoryInfo;
        @BindView(R.id.inventory_inventoryInfo)
        TextView inventoryInventoryInfo;
        @BindView(R.id.num_inventoryInfo)
        TextView numInventoryInfo;
        @BindView(R.id.purchasePrice_inventoryInfo)
        TextView purchasePriceInventoryInfo;
        @BindView(R.id.total_inventoryInfo)
        TextView totalInventoryInfo;
        @BindView(R.id.info_inventory_order_number)
        TextView infoInventoryOrderNumber;
        @BindView(R.id.ll_order_number)
        LinearLayout llOrderNumber;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
